package com.springcloud.demo.prac_volatile;

/**
 * @author zhouyang
 * @Description
 * @date 2018/09/13
 */
public class Test {

    static volatile boolean flag = true;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    flag = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "打断");
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0; i<10; i++){
                        if(flag) {
                            System.out.println(Thread.currentThread().getName() + " -------------------- " + i);
                            Thread.sleep(1000);
                        }else{
                            return;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
